public without sharing class WorkOrderModifiyController {


	public String msg {private set ;get;}
	public String backId {private set; get;}
	public String dataJson {private set; get;}
    public String saveJson { set; get;}

	public Boolean isSuc {private set; get;}
	public Boolean isErr {private set; get;}


	private WorkOrder__c wo  = new WorkOrder__c(); 
	private Map<String,DataUtil.SObjectBean> stageObjectMap;

    public WorkOrderModifiyController() {
        backId = System.currentPageReference().getParameters().get('id');
       stageObjectMap = new Map<String,DataUtil.SObjectBean>();

    }


     public Map<String,Set<String>>  getFields(){
        Map<String,Set<String>> fieldsMap  = new  Map<String,Set<String>>();
        
            DataUtil.SObjectBean sb = new DataUtil.SObjectBean();

            sb.title ='';
            sb.objname = 'WorkOrder__c';
            sb.stype = DataUtil.getSObjectType( sb.objname );
           	sb.idvalue =backId;
            sb.type = 'base';
            sb.condition = ' where Id =\''+backId+'\'';
            stageObjectMap.put( sb.objname ,sb );
            Set<String>  fields = new Set<String>{'Reason_Desc__c' ,'ResultsSuggestions__c' ,'ServiceSituation__c','ServiceTrackingNotes__c','WorkOrderCategory__c','PDFReport__c'};
        
            fieldsMap.put( sb.objname , fields);
        
        return fieldsMap;
    }


    public void init(){

    	try{

    		 List<User> usl = [select Id from User where  Id=:UserInfo.getUserId() and (Name in: Label.ModifyWorkOrder.split(';') or Profile.Name = '系统管理员') ];

    		 if(usl.isEmpty() && !System.Test.isRunningTest()) throw new DmlException('没有权限!');
	    	//wo = [select Id,Reason_Desc__c ,ResultsSuggestions__c ,WorkOrderCategory__c,ServiceSituation__c,ServiceTrackingNotes__c,PDFReport__c from WorkOrder__c  where Id =: backId];
	    	 Map<String,Object> dataMap = new Map<String,Object>();
	    	 //字段
	         Map<String,Set<String>> fieldsmap = getFields();
	         //字段详情
	         Map<String, List<DataUtil.FieldBean>> fbMap = DataUtil.getFieldsBean(fieldsmap,stageObjectMap);
	        //字段赋值
	         Map<String,List<DataUtil.FieldBean>> fields = DataUtil.setFieldsValue(fbMap,DataUtil.getBaseInfo(fbMap,stageObjectMap) );
	        
	        dataMap.put('base',fields);
	        dataMap.put('baseObject',DataUtil.getBaseObjectMap(stageObjectMap));

	        dataJson = EncodingUtil.urlEncode(JSON.serialize(dataMap), 'UTF-8');

    	}catch(Exception e ){

    		isErr = true;
            ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()+e.getStackTraceString()));
    	}

    	




    }



    public void save(){



    	try{

    		Map<String,Object> saveMap = (Map<String,Object>  )  JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));


	    	wo.Reason_Desc__c = (String)saveMap.get('Reason_Desc__c');
	    	wo.ResultsSuggestions__c = (String)saveMap.get('ResultsSuggestions__c');
	    	wo.WorkOrderCategory__c =(String) saveMap.get('WorkOrderCategory__c');
	    	wo.ServiceSituation__c = (String)saveMap.get('ServiceSituation__c');
	    	wo.ServiceTrackingNotes__c = (String)saveMap.get('ServiceTrackingNotes__c');
	    	wo.PDFReport__c = (String) saveMap.get('PDFReport__c');
	    	wo.Id = backId;
	    	update wo ; 
	    	isSuc = true;
	    	msg = '保存成功!';


    	}catch(Exception e ){
    		msg = '保存失败:'+e.getMessage();

    		isSuc = false;
    		
    	}




    }



}